import com.lagou.dao.IUserDao;
import com.lagou.service.IUserService;
import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;

public class test1 {
    @Test
    public void springTest(){
        // 1.获取spring上下文对象，加载的同时创建了bean对象放在了容器中
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        // 2.借助上下文对象获取到IOC容器中的bean对象
        IUserDao userDao = (IUserDao) context.getBean("userDao");
        userDao.save();
    }
    @Test
    public void springTest2(){
        // 1.核心接口,不会创建bean放到容器中
        BeanFactory xmlBeanFactory = new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));
        // 2.getBean的时候才会创建bean对象,然后又从容器中拿想要的bean
        IUserDao userDao = (IUserDao) xmlBeanFactory.getBean("userDao");
        userDao.save();
    }
    @Test
    public void springTest3(){
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        IUserService userService = (IUserService) context.getBean("userService");
        userService.save();
    }
}
